package com.lims.landInfo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import javacommon.base.BaseStruts2Action;
import javacommon.excelUtils.XmlReader;
import javacommon.util.TextTool;

import com.lims.landInfo.service.BookManager;
import com.lims.landInfo.service.CollectiveManager;
import com.lims.landInfo.service.CorpaffManager;
import com.lims.landInfo.service.CorporationManager;
import com.lims.landInfo.service.DisposalManager;
import com.lims.landInfo.service.GuaranteeManager;
import com.lims.landInfo.service.HouseManager;
import com.lims.landInfo.service.HouseRentManager;
import com.lims.landInfo.service.InformationManager;
import com.lims.landInfo.service.LandManager;
import com.lims.landInfo.service.LandUseManager;
import com.lims.landInfo.service.LawsuitManager;
import com.lims.landInfo.service.LogoutManager;
import com.lims.landInfo.service.ProjectManager;
import com.lims.landInfo.service.RealEstateManager;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings({ "unchecked", "serial"})
public class ExcelAction extends BaseStruts2Action implements Preparable,ModelDriven{
	private static Logger logger = Logger.getLogger(ExcelAction.class);
	
	protected static final String FINISH_JSP = "/pages/landInfo/importFinish.jsp";
	private static final String IMPORTFORM_JSP = "/pages/landInfo/importForm.jsp";

	public void prepare() throws Exception {
	}

	public Object getModel() {
		return null;
	}
	
	private File uploadFile;//得到上传的文件
	private String uploadFileContentType;//得到文件的类型
	private String uploadFileFileName;//得到文件的名称
	private String beanName;
	private Date landdate;

	private BookManager bookManager;
	private CollectiveManager collectiveManager;
	private CorporationManager corporationManager;
	private CorpaffManager corpaffManager;
	private DisposalManager disposalManager;
	private GuaranteeManager guaranteeManager;
	private HouseManager houseManager;
	private HouseRentManager houseRentManager;
	private InformationManager informationManager;
	private LandManager landManager;
	private LandUseManager landUseManager;
	private LawsuitManager lawsuitManager;
	private LogoutManager logoutManager;
	private ProjectManager projectManager;
	private RealEstateManager realEstateManager;
	
	/** 增加setXXXX()方法 注入 */
	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}

	public void setCollectiveManager(CollectiveManager collectiveManager) {
		this.collectiveManager = collectiveManager;
	}

	public void setCorporationManager(CorporationManager corporationManager) {
		this.corporationManager = corporationManager;
	}

	public void setCorpaffManager(CorpaffManager corpaffManager) {
		this.corpaffManager = corpaffManager;
	}

	public void setDisposalManager(DisposalManager disposalManager) {
		this.disposalManager = disposalManager;
	}

	public void setGuaranteeManager(GuaranteeManager guaranteeManager) {
		this.guaranteeManager = guaranteeManager;
	}

	public void setHouseManager(HouseManager houseManager) {
		this.houseManager = houseManager;
	}

	public void setHouseRentManager(HouseRentManager houseRentManager) {
		this.houseRentManager = houseRentManager;
	}

	public void setInformationManager(InformationManager informationManager) {
		this.informationManager = informationManager;
	}

	public void setLandManager(LandManager landManager) {
		this.landManager = landManager;
	}

	public void setLandUseManager(LandUseManager landUseManager) {
		this.landUseManager = landUseManager;
	}

	public void setLawsuitManager(LawsuitManager lawsuitManager) {
		this.lawsuitManager = lawsuitManager;
	}

	public void setLogoutManager(LogoutManager logoutManager) {
		this.logoutManager = logoutManager;
	}

	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	public void setRealEstateManager(RealEstateManager realEstateManager) {
		this.realEstateManager = realEstateManager;
	}

	/**
	 * 跳转至上传界面
	 * @return
	 */
	public String importForm() {
		return IMPORTFORM_JSP;
	}
	

	/**导入Excel
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws FileNotFoundException 
	 * ***/
	public String importExcel() throws InstantiationException, IllegalAccessException, ClassNotFoundException, FileNotFoundException {
		String importSuccessMsg = "";
		String importFailMsg = "";
		List errList = null;
		if(!isNullOrEmptyString(beanName)) {
			logger.info("####开始导入数据：导入时间【"+TextTool.dateToStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss")+"】");
			FileInputStream fis = new FileInputStream(uploadFile);
			List list = XmlReader.ExcelToList(fis, beanName);
			int totalRow = list.size();
			if(TextTool.isNotNull(list)) {
				if("Book".equals(beanName)) {
					errList = this.bookManager.saveAndCheckImpData(list,landdate);
				} else if("Collective".equals(beanName)) {
					errList = this.collectiveManager.saveAndCheckImpData(list,landdate);
				} else if("Disposal".equals(beanName)) {
					errList = this.disposalManager.saveAndCheckImpData(list,landdate);
				} else if("Guarantee".equals(beanName)) {
					errList = this.guaranteeManager.saveAndCheckImpData(list,landdate);
				} else if("House".equals(beanName)) {
					errList = this.houseManager.saveAndCheckImpData(list,landdate);
				} else if("HouseRent".equals(beanName)) {
					errList = this.houseRentManager.saveAndCheckImpData(list,landdate);
				} else if("Information".equals(beanName)) {
					errList = this.informationManager.saveAndCheckImpData(list,landdate);
				} else if("Lawsuit".equals(beanName)) {
					errList = this.lawsuitManager.saveAndCheckImpData(list,landdate);
				} else if("Logout".equals(beanName)) {
					errList = this.logoutManager.saveAndCheckImpData(list,landdate);
				} else if("Project".equals(beanName)) {
					errList = this.projectManager.saveAndCheckImpData(list,landdate);
				} else if("Land".equals(beanName)) {
					errList = this.landManager.saveAndCheckImpData(list,landdate);
				} else if("HouseRent".equals(beanName)) {
					errList = this.houseRentManager.saveAndCheckImpData(list,landdate);
				} else if("LandUse".equals(beanName)) {
					errList = this.landUseManager.saveAndCheckImpData(list,landdate);
				} else if("RealEstate".equals(beanName)) {
					errList = this.realEstateManager.saveAndCheckImpData(list,landdate);
				} else if("Corporation".equals(beanName)) {//单位
					try {
						this.corporationManager.saveAll(list);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if("Corpaff".equals(beanName)) { //上下级关系
					this.corpaffManager.saveAll(list);
				}
				//组装导入成功或失败信息
				if(TextTool.isNull(errList)) {
					importSuccessMsg += "导入成功"+totalRow+"条。";
					importFailMsg += "导入失败0条。";
				} else {
					importSuccessMsg += "导入成功"+(totalRow-errList.size())+"条。";
					importFailMsg += "导入失败"+errList.size()+"条。";
				}
			} else {
				importFailMsg = "导入失败，请检查上传文件。";
			}
			logger.info("####开始导入完成：完成时间【"+TextTool.dateToStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss")+"】");
		}
		this.requestMap.put("importSuccessMsg", importSuccessMsg);
		this.requestMap.put("importFailMsg", importFailMsg);
		//把对象的备注 字段做为错误显示
		this.requestMap.put("errList", errList);
		return FINISH_JSP;
	}
	
	
	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
	
	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	
	public Date getLanddate() {
		return landdate;
	}

	public void setLanddate(Date landdate) {
		this.landdate = landdate;
	}
}
